home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 03 control flow and error handling / controlflowdemo / mainmodule1.vb < prev    next >
Encoding:
Text File  |  2002-03-20  |  8.4 KB  |  251 lines

  1. Module MainModule
  2.  
  3.     Sub Main()
  4.         ' Run one of the Textxxxx procedures below by uncommenting only one statement
  5.  
  6.         'TestReturn()
  7.         'TestCallByRef()
  8.         'TestArrayByVal()
  9.         'TestArrayByRef()
  10.         'TestParamArray()
  11.         'TestLabelProblem()
  12.         'TestFindWindow()
  13.         'TestVBFunctions()
  14.  
  15.         ' These statements are usuful when running inside Visual Studio.NET
  16.         Console.WriteLine("")
  17.         Console.WriteLine(">>> Press Enter to terminate the program <<<")
  18.         Console.ReadLine()
  19.     End Sub
  20.  
  21.     ' this procedure tests the Return performance compared to traditional
  22.     ' way to return from a procedure
  23.  
  24.     Sub TestReturn()
  25.         Dim start As Date
  26.         Dim i As Integer
  27.         Dim res As Integer
  28.  
  29.         start = Now
  30.         For i = -10000000 To 10000000
  31.             res = TestFunction_One(i)
  32.         Next
  33.         Console.WriteLine("Using local variable: {0} secs.", Now.Subtract(start))
  34.  
  35.         start = Now
  36.         For i = -10000000 To 10000000
  37.             res = TestFunction_Two(i)
  38.         Next
  39.         Console.WriteLine("Using Return statement: {0} secs.", Now.Subtract(start))
  40.     End Sub
  41.  
  42.     ' auxiliary functions
  43.  
  44.     Function TestFunction_One(ByVal x As Integer) As Integer
  45.         If x > 0 Then
  46.             TestFunction_One = 1
  47.         Else
  48.             TestFunction_One = -1
  49.         End If
  50.     End Function
  51.  
  52.     Function TestFunction_Two(ByVal x As Integer) As Integer
  53.         If x > 0 Then
  54.             Return 1
  55.         Else
  56.             Return -1
  57.         End If
  58.     End Function
  59.  
  60.     ' this procedure demonstrates that array passed ByVal can be
  61.     ' modified by the called procedure
  62.  
  63.     Sub TestArrayByVal()
  64.         Dim anArray() As Integer = {0, 1, 2, 3, 4}
  65.         ' Pass the array by value to a procedure.
  66.         Call ArrayProcByval(anArray)
  67.         ' Prove that the array has been modified.
  68.         Console.WriteLine(anArray(3))     ' => 300
  69.     End Sub
  70.  
  71.     ' A procedure that modifies its array argument's elements
  72.     Sub ArrayProcByval(ByVal arr() As Integer)
  73.         Dim i As Integer
  74.         For i = 0 To UBound(arr)
  75.             arr(i) = arr(i) * 100
  76.         Next
  77.     End Sub
  78.  
  79.     ' this procedure shows the difference in passing an array byval or byref
  80.  
  81.     Sub TestArrayByRef()
  82.         Dim byvalArray(10) As Integer
  83.         Dim byrefArray(10) As Integer
  84.         ' Pass both arrays to the procedure.
  85.         Call ArrayProcByRef(byvalArray, byrefArray)
  86.         ' Check which array has been affected by the ReDim.
  87.         Console.WriteLine(UBound(byvalArray))   ' => 10 (not modified)
  88.         Console.WriteLine(UBound(byrefArray))   ' => 100 (modified)
  89.     End Sub
  90.  
  91.     Sub ArrayProcByRef(ByVal arr() As Integer, ByRef arr2() As Integer)
  92.         ' Change the size of both arrays.
  93.         ReDim arr(100)
  94.         ReDim arr2(100)
  95.     End Sub
  96.  
  97.     ' this procedure tests the ParamArray keyword
  98.  
  99.     Sub TestParamArray()
  100.         Console.WriteLine(Sum(3, 1, 5))            ' => 9
  101.  
  102.         Console.WriteLine(Sum2(3, 1, 5))           ' => 9
  103.  
  104.         Console.WriteLine(MinValue(3, 1, 5))       ' => 1
  105.     End Sub
  106.  
  107.     ' sum all the arguments
  108.     Function Sum(ByVal ParamArray args() As Integer) As Integer
  109.         Dim sumResult, index As Integer
  110.         For index = 0 To UBound(args)
  111.             sumResult += args(index)
  112.         Next
  113.         Sum = sumResult
  114.     End Function
  115.  
  116.     ' alternative, more concise version
  117.     Function Sum2(ByVal ParamArray args() As Integer) As Integer
  118.         Dim index As Integer
  119.         For index = 0 To UBound(args)
  120.             Sum2 = Sum2 + args(index)
  121.         Next
  122.     End Function
  123.  
  124.     ' this procedure shows that you can deal with a ParamArray argument
  125.     ' as if it were a regular array
  126.  
  127.     Function MinValue(ByVal ParamArray args() As Object) As Object
  128.         ' Sort the array, and then return its 1st element.
  129.         System.Array.Sort(args)
  130.         MinValue = args(0)
  131.     End Function
  132.  
  133.     ' this procedure demonstrates a problem with procedures without any arguments:
  134.  
  135.     Sub TestLabelProblem()
  136.         ' This code doesn't work as expected.
  137. WriteHello: Console.WriteLine("World") ' => World
  138.  
  139.         ' This code works correctly.
  140.         WriteHello() : Console.WriteLine("World") ' => Hello World
  141.     End Sub
  142.  
  143.     Sub WriteHello()
  144.         Console.Write("Hello ")
  145.     End Sub
  146.  
  147.     ' API declares
  148.  
  149.     Private Declare Ansi Function FindWindow Lib "user32" Alias "FindWindowA" _
  150.         (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
  151.     Private Declare Function MoveWindow Lib "user32" Alias "MoveWindow" _
  152.         (ByVal hWnd As Integer, ByVal x As Integer, ByVal y As Integer, _
  153.         ByVal nWidth As Integer, ByVal nHeight As Integer, _
  154.         ByVal bRepaint As Integer) As Integer
  155.  
  156.     ' NOTE: launch Notepad on an empty document before running this code.
  157.     Sub TestFindWindow()
  158.         Dim hWnd As Integer = FindWindow(Nothing, "Untitled - Notepad")
  159.         If hWnd <> 0 Then MoveWindow(hWnd, 0, 0, 600, 300, 1)
  160.     End Sub
  161.  
  162.     ' this procedure tests several functions in the Microsoft.VisualBasic namespaces
  163.  
  164.     Sub TestVBFunctions()
  165.         Dim s1 As String = "abc"
  166.         Dim s2 As String = "ABC"
  167.         Dim res As String
  168.  
  169.         ' Compare two strings in case-insensitive mode.
  170.         If StrComp(s1, s2, CompareMethod.Text) = 0 Then res = "Equal"
  171.         Console.WriteLine("res = " & res)
  172.  
  173.         ' This statements prints a line of 50 dashes.
  174.         Console.WriteLine(StrDup(50, "-"))
  175.  
  176.         ' the GetChar method
  177.         Console.WriteLine(GetChar("ABCDE", 2))     ' => B
  178.  
  179.         ' The natural logarithm of 10 
  180.         Console.WriteLine(Math.Log(10))            ' => 2.30258509299405
  181.         ' Two ways to evaluate he decimal logarithm of 1000
  182.         Console.WriteLine(Math.Log(1000, 10))      ' => 3
  183.         Console.WriteLine(Math.Log10(1000))        ' => 3
  184.  
  185.         Console.WriteLine(Math.Min(1.5, 0.7))      ' => 0.7
  186.         Console.WriteLine(Math.Max(99, 87))        ' => 99
  187.  
  188.         Console.WriteLine(Math.Floor(-1.5))        ' => -2
  189.         Console.WriteLine(Math.Ceiling(2.5))       ' => 3
  190.  
  191.         Console.WriteLine(Math.IEEERemainder(2, 1.5))      ' => 0.5 
  192.  
  193.         ' date-time functions
  194.  
  195.         ' Get the date two weeks from now.
  196.         Dim newDate As Date = DateAdd(DateInterval.WeekOfYear, 2, Now())
  197.         Console.WriteLine(newDate)
  198.  
  199.         ' Evaluate days left to December 31 of current year.
  200.         Dim days As Long = DateDiff(DateInterval.Day, Today, _
  201.             DateSerial(Year(Today), 12, 31))
  202.         Console.WriteLine(days)
  203.  
  204.         ' abbreviated month name
  205.         Console.WriteLine(MonthName(1, True))       ' => Jan
  206.  
  207.         ' display a msgbox
  208.  
  209.         Dim userChoice As MsgBoxResult
  210.         userChoice = MsgBox("Want to run Notepad", MsgBoxStyle.Question Or MsgBoxStyle.YesNo)
  211.         If userChoice = MsgBoxResult.Yes Then
  212.             ' run notepad and wait until it returns, or until 10 secs. have elapsed
  213.             Dim taskID As Long = Shell("notepad", AppWinStyle.NormalFocus, True, 10000)
  214.             If taskID = 0 Then
  215.                 Console.WriteLine("Notepad has been closed within 10 seconds.")
  216.             Else
  217.                 Console.WriteLine("Notepad is still running after 10 seconds.")
  218.             End If
  219.         End If
  220.  
  221.         ' read a text file 
  222.         Dim handle As Integer = FreeFile()
  223.         ' open a file for input
  224.         FileOpen(handle, "C:\autoexec.bat", OpenMode.Input, OpenAccess.Read)
  225.         ' read the entire file in one operation
  226.         Dim fileText As String = InputString(handle, CInt(LOF(handle)))
  227.         ' close the file
  228.         FileClose(handle)
  229.         Console.WriteLine(fileText)
  230.  
  231.         ' Display the description associated to error code 9.
  232.         Console.WriteLine(ErrorToString(9))   ' => Subscript out of Range
  233.  
  234.         Dim x As Integer
  235.         Console.WriteLine(System.Runtime.InteropServices.Marshal.SizeOf(x))      ' => 4
  236.  
  237.         Console.WriteLine(SystemTypeName("Long"))        ' => System.Int64
  238.         Console.WriteLine(VbTypeName("System.Int16"))    ' => Short
  239.  
  240.         ' The new IsReference function returns True if the argument evaluates to a reference type,
  241.         '  or False if it is a value type:
  242.         Dim n As Integer = 1
  243.         Dim s As String = "ABC"
  244.         Console.WriteLine(IsReference(n))    ' => False
  245.         Console.WriteLine(IsReference(s))    ' => True
  246.     End Sub
  247.  
  248. End Module
  249.  
  250.  
  251.